package cn.piaoyi.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.Category;
import cn.piaoyi.bean.Daily;
import cn.piaoyi.bean.Goods;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.CategoryService;
import cn.piaoyi.service.DailyService;
import cn.piaoyi.service.GoodsService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;
import cn.piaoyi.web.servlet.AdminSession;
import cn.piaoyi.web.ui.OptionsInt;
import cn.piaoyi.web.ui.OptionsLong;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class AdminDaily extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminDaily.class);
	
	private GoodsService goodsService;
	private DailyService dailyService;

	public GoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}
	
	public DailyService getDailyService() {
		return dailyService;
	}

	public void setDailyService(DailyService dailyService) {
		this.dailyService = dailyService;
	}
	
	private List startWeekList = new ArrayList();
	private List limitTimeList = new ArrayList();

	public List getStartWeekList() {
		return startWeekList;
	}

	public void setStartWeekList(List startWeekList) {
		this.startWeekList = startWeekList;
	}
	
	public List getLimitTimeList() {
		return limitTimeList;
	}

	public void setLimitTimeList(List limitTimeList) {
		this.limitTimeList = limitTimeList;
	}

	private void setStartWeekListValues(){
		startWeekList.add(new OptionsInt(-1, "请选择"));
		startWeekList.add(new OptionsInt(1, "星期一"));
		startWeekList.add(new OptionsInt(2, "星期二"));
		startWeekList.add(new OptionsInt(3, "星期三"));
		startWeekList.add(new OptionsInt(4, "星期四"));
		startWeekList.add(new OptionsInt(5, "星期五"));
		startWeekList.add(new OptionsInt(6, "星期六"));
		startWeekList.add(new OptionsInt(0, "星期日"));
		
	}
	private void setLimitTimeListValues(){
		limitTimeList = Constant.GOODS_LIMIT_TIME;
	}
	private void initValues(){
		setStartWeekListValues();
		setLimitTimeListValues();
	}


	private long id;			//主键ID
	private int limitTime;		//时间设置 0:不限时间,1指定时间
	private long goodsId;		//商品ID
	private Date startTime;		//开始时间
	private int startWeek = -1;		//星期
	private int startHour;		//开始小时
	private int startMinute;	//开始分钟

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getLimitTime() {
		return limitTime;
	}

	public void setLimitTime(int limitTime) {
		this.limitTime = limitTime;
	}

	public long getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(long goodsId) {
		this.goodsId = goodsId;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public int getStartWeek() {
		return startWeek;
	}

	public void setStartWeek(int startWeek) {
		this.startWeek = startWeek;
	}

	public int getStartHour() {
		return startHour;
	}

	public void setStartHour(int startHour) {
		this.startHour = startHour;
	}

	public int getStartMinute() {
		return startMinute;
	}

	public void setStartMinute(int startMinute) {
		this.startMinute = startMinute;
	}
	
	private List dailyList;
	private Goods goods;

	public List getDailyList() {
		return dailyList;
	}

	public void setDailyList(List dailyList) {
		this.dailyList = dailyList;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		this.setGoods(this.getGoodsService().findGoodsById(this.goodsId));
		this.setDailyList(this.getDailyService().findDailys(this.goodsId, "id", 0));
		return SUCCESS;
	}
	
	public String add(){
		initValues();
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		//添加场次
		Daily d = new Daily();
		d.setLimitTime(this.limitTime);
		d.setGoodsId(this.goodsId);
		if(this.limitTime == 0){
			d.setStartTime(new Date());
			d.setStartWeek(0);
			d.setStartHour(0);
			d.setStartMinute(0);
		}else{
			d.setStartTime(this.startTime);
			d.setStartWeek(this.startWeek);
			d.setStartHour(this.startHour);
			d.setStartMinute(this.startMinute);
		}
		try {
			this.getDailyService().createDaily(d);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		initValues();
		this.setAction("doupdate");
		Daily d = this.getDailyService().findDailyById(this.id);
		this.setLimitTime(d.getLimitTime());
		this.setGoodsId(d.getGoodsId());
		this.setStartTime(d.getStartTime());
		this.setStartWeek(d.getStartWeek());
		this.setStartHour(d.getStartHour());
		this.setStartMinute(d.getStartMinute());
		return INPUT;
	}
	
	public String doupdate(){
		//添加场次
		Daily d = this.getDailyService().findDailyById(this.id);
		d.setLimitTime(this.limitTime);
		d.setGoodsId(this.goodsId);
		if(this.limitTime == 0){
			d.setStartTime(new Date());
			d.setStartWeek(0);
			d.setStartHour(0);
			d.setStartMinute(0);
		}else{
			d.setStartTime(this.startTime);
			d.setStartWeek(this.startWeek);
			d.setStartHour(this.startHour);
			d.setStartMinute(this.startMinute);
		}
		try {
			this.getDailyService().updateDaily(d);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getDailyService().removeDailyById(this.id);
		return "index";
	}

}
